package xyz.jcat.biz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import xyz.jcat.biz.admin.domain.dto.RoleAddDto;
import xyz.jcat.common.util.ObjectUtils;
import xyz.jcat.common.web.RespCodeMsg;
import xyz.jcat.common.web.RespException;
import xyz.jcat.mybatis.ServiceImpl;
import xyz.jcat.biz.admin.domain.po.RolePo;
import xyz.jcat.biz.admin.domain.dao.IRoleDao;
import xyz.jcat.biz.admin.service.IRoleService;
/**
 *
 * @author yueyue
 * auto on 2022-04-28 13:41:47
 */
@Service
public class RoleService extends ServiceImpl<IRoleDao, RolePo> implements IRoleService {

    private RolePo getByRoleName(String roleName) {
        LambdaQueryWrapper<RolePo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RolePo::getRoleName, roleName);
        return getOne(lambdaQueryWrapper);
    }
    @Override
    public void addRole(RoleAddDto roleAddDto) {
        RolePo oldRole = getByRoleName(roleAddDto.getRoleName());
        if(ObjectUtils.notNull(oldRole)) {
            throw new RespException(RespCodeMsg.DATA_EXIST, "角色名称已存在");
        }
        RolePo role = new RolePo();
        role.setRoleName(roleAddDto.getRoleName());
        insert(role);
    }
}
